package router

import (
	"autoDeploy/controllers/Admin"
	"autoDeploy/controllers/UserProject"
	"autoDeploy/controllers/emails"
	"autoDeploy/controllers/log"
	"autoDeploy/controllers/login"
	"autoDeploy/controllers/qrcode"
	"autoDeploy/controllers/register"
	"autoDeploy/controllers/route"
	"autoDeploy/controllers/userTeam"
	"autoDeploy/middleware"
	"github.com/gin-gonic/gin"
)

func Router() *gin.Engine {
	r := gin.Default()
	//加载html模板文件
	r.LoadHTMLGlob("templates/*")

	r.Use(middleware.CORSMiddleware())
	// 注册全局中间件
	r.Use(func(c *gin.Context) {
		c.Writer.Header().Set("Content-Type", "application/json; charset=utf-8")
		c.Next()
	})

	//管理员相关请求
	admin := r.Group("/admin")
	admin.Use(middleware.AdminAuthMiddleware())
	{
		admin.GET("/containers", Admin.Admin{}.GetContainers)
		admin.GET("/teams", Admin.Admin{}.GetTeams)
		//解散团队
		admin.DELETE("/deleteTeam", Admin.Admin{}.DeleteTeam)
		//停止容器
		admin.PUT("/stopContainer", Admin.Admin{}.StopContainer)
		//运行容器
		admin.PUT("/startContainer", Admin.Admin{}.RunContainer)
	}

	//登录注册相关的请求
	user := r.Group("/user")
	{
		user.GET("/login", login.UserInfo{}.Login)
		user.POST("/register", register.UserRegisterInfo{}.Register)
		user.GET("/captcha", register.UserRegisterInfo{}.GetCode)
	}

	//团队相关路由
	team := r.Group("/team")
	team.Use(middleware.AuthMiddleware())
	{
		//初始时，加载页面数据
		team.GET("/getResource", userTeam.Team{}.GetUserResource)
		//用户团队相关 新建团队
		team.POST("/addUserTeam", userTeam.Team{}.AddUserTeam)
		//发送邀请信息 邀请成员
		team.POST("/inviteMember", userTeam.Team{}.InviteMember)
		//团队管理  管理
		team.GET("/manageTeam", userTeam.Team{}.ManageTeam)
		//新建项目
		team.POST("/addNewProject", userTeam.Team{}.AddNewProject)
		//同意入队
		team.POST("/agree", userTeam.Team{}.Agree)
		//移除成员
		team.POST("/drop", userTeam.Team{}.Drop)
		//统计项目数量
		team.GET("/getTeamSum", userTeam.Team{}.GetTeamSum)
	}

	//用户项目相关的路由
	project := r.Group("/project")
	project.Use(middleware.AuthMiddleware())
	{
		//初始化加载用户的所有项目信息
		project.GET("/getResource", UserProject.UserProject{}.GetProject)
		//部署项目
		project.POST("/deploy", UserProject.UserProject{}.Deploy)
		//查看项目ip及端口
		project.GET("/getPort", UserProject.UserProject{}.GetPort)
		//统计项目团队数量
		project.GET("/getProjectSum", UserProject.UserProject{}.GetProjectSum)
		//统计成员个数
		project.GET("/getMemberSum", UserProject.UserProject{}.GetMemberSum)

	}
	//webhook消息推送
	r.POST("/webhook", UserProject.UserProject{}.WebHook)

	//邮件相关路由
	email := r.Group("/email")
	email.Use(middleware.AuthMiddleware())
	{
		//查看所有的电子邮件
		email.GET("/getAllEmail", emails.Email{}.GetEmails)
	}

	//日志相关路由
	logs := r.Group("/log")
	logs.Use(middleware.AuthMiddleware())
	{
		//获取全部日志
		logs.GET("/getLogs", log.Log{}.GetLog)
	}

	//根据角色token获取不同的路由列表
	roleRouter := r.Group("/role")
	roleRouter.Use(middleware.RouterAuthMiddleware())
	{
		roleRouter.GET("/getRoutes", route.Route{}.GetRoute)
	}

	//二维码相关的请求
	scan := r.Group("/qrcode")
	scan.Use(middleware.AuthMiddleware())
	{
		scan.GET("/getQrcode", qrcode.GetQRCode)     //获取二维码
		scan.GET("/docker", qrcode.ScanDocker)       //访问模拟购买
		scan.GET("/k8s", qrcode.ScanK8S)             //访问模拟购买
		scan.GET("/submitPhone", qrcode.SubmitPhone) //提交订单类型和手机号
	}

	return r
}
